package com.eastfair.pay.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.eastfair.annotation.annotation.base.BaseControllerInfo;
import com.eastfair.annotation.annotation.security.PreAuth;
import com.eastfair.boot.controller.SuperController;
import com.eastfair.boot.request.PageParams;
import com.eastfair.core.base.R;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.core.exception.BizException;
import com.eastfair.echo.core.EchoService;
import com.eastfair.pay.demand.CollectRecordGroup;
import com.eastfair.pay.dto.*;
import com.eastfair.pay.entity.CollectRecord;
import com.eastfair.pay.service.CollectRecordService;
import com.eastfair.pay.vo.CollectPageDetailVO;
import com.eastfair.pay.vo.CollectRecordVO;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * <p>
 * 前端控制器
 * 认款记录
 * </p>
 *
 * @author clm
 * @date 2022-06-08
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/collectRecord")
@Api(value = "CollectRecord", tags = "认款记录")
@PreAuth(replace = "pay:collectRecord:")
public class CollectRecordController extends SuperController<CollectRecordService, Long, CollectRecord, CollectRecordPageQuery, CollectRecordSaveDTO, CollectRecordUpdateDTO> {

    @Autowired
    private EchoService echoService;

    @Autowired
    private CollectRecordService collectRecordService;

    @Override
    public void handlerResult(IPage<CollectRecord> page) {
        // 想让返回值实现自动回显，请将此行代码打开
        // echoService.action(page);
    }

    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list) {
        List<CollectRecord> collectRecordList = list.stream().map((map) -> {
            CollectRecord collectRecord = CollectRecord.builder().build();
            //TODO 请在这里完成转换
            return collectRecord;
        }).collect(Collectors.toList());

        return R.success(baseService.saveBatch(collectRecordList));
    }


    /**
     * 处理参数
     *
     * @param params 分页参数
     */
    @Override
    public void handlerQueryParams(PageParams<CollectRecordPageQuery> params) {
        CollectRecordPageQuery pageQuery = params.getModel();
        //分页处理
        if (ContextUtil.getProjectId() != null) {
            pageQuery.setProjectId(ContextUtil.getProjectId());
        }
    }

    /**
     * 认领
     *
     * @return
     */
    @PostMapping("/collect")
    public R collect(@RequestBody CollectDTO collectDTO) {

        try {
            collectRecordService.collect(collectDTO);
            return R.success();
        } catch (BizException biz) {
            return R.fail(biz.getCode(), biz.getMessage());
        }
    }

    /**
     * 认款（餐饮，展览，会议）
     *
     * @return
     */
    @PostMapping("/ordinary/collect")
    public R ordinaryCollect(OrdinaryCollectDTO collectDTO) {
        try {
            collectRecordService.ordinaryCollect(collectDTO);
            return R.success();
        } catch (BizException biz) {
            return R.fail(biz.getCode(), biz.getMessage());
        }

    }

    /**
     * 通过订单号查询认款记录
     *
     * @param orderNum
     * @return
     */
    @GetMapping("/queryCollectRecord/{orderNum}")
    public R<List<CollectRecordVO>> queryCollectRecordByOrderNum(@PathVariable("orderNum") String orderNum) {
        return R.success(collectRecordService.queryCollectRecordByOrderNum(orderNum));
    }

    /**
     * 取消认款（餐饮，展览，会议）
     *
     * @param collectRecordId
     * @return
     */
    @GetMapping("/ordinary/cancel/{collectRecordId}")
    public R ordinaryCancelCollect(@PathVariable("collectRecordId") Long collectRecordId) {
        UnCollectDTO unCollectDTO = new UnCollectDTO().setCollectRecordId(collectRecordId);
        collectRecordService.commonUncollect(unCollectDTO);
        return R.success();
    }


    /**
     * 取消认领
     *
     * @return
     */
    @PostMapping("/uncollect")
    public R uncollect(@RequestBody UnCollectDTO unCollectDTO) {

        try {
            collectRecordService.unCollect(unCollectDTO);
            return R.success();
        } catch (BizException biz) {
            return R.fail(biz.getCode(), biz.getMessage());
        }

    }


    /**
     * 取消认领
     *
     * @return
     */
    @PostMapping("/commonUncollect")
    public R commonUncollect(@RequestBody UnCollectDTO unCollectDTO) {

        try {
            collectRecordService.commonUncollect(unCollectDTO);
            return R.success();
        } catch (BizException biz) {
            return R.fail(biz.getCode(), biz.getMessage());
        }

    }

    /**
     * 获取付款计划认领记录
     *
     * @param paymentPlanId
     * @return
     */
    @GetMapping("/queryCollectByPlanId/{paymentPlanId}")
    @BaseControllerInfo(voGroup = {CollectRecordGroup.queryCollectByPlanId.class})
    public R<List<CollectRecordVO>> queryCollectByPlanId(@PathVariable("paymentPlanId") Long paymentPlanId) {

        try {
            List<CollectRecordVO> collectRecordVOs = collectRecordService.queryCollectByPlanId(paymentPlanId);
            return R.success(collectRecordVOs);
        } catch (BizException biz) {
            return R.fail(biz.getCode(), biz.getMessage());
        }

    }

    /**
     * 获取付款记录认领记录
     *
     * @param recordId
     * @return
     */
    @GetMapping("/queryCollectByRecordId/{recordId}")
    @BaseControllerInfo(voGroup = {CollectRecordGroup.queryCollectByRecordId.class})
    public R<List<CollectRecordVO>> queryCollectByRecordId(@PathVariable("recordId") Long recordId) {

        try {
            List<CollectRecordVO> collectRecordVOs = collectRecordService.queryCollectByRecordId(recordId);
            return R.success(collectRecordVOs);
        } catch (BizException biz) {
            return R.fail(biz.getCode(), biz.getMessage());
        }

    }

    /**
     * 通过合同获取认领页面详情
     *
     * @param businessId
     * @return
     */
    @GetMapping("/queryCollectPageDetail/{businessId}")
    public R<CollectPageDetailVO> getCollectPageDetail(@PathVariable("businessId") Long businessId) {
        try {
            CollectPageDetailVO collectPageDetailVO = collectRecordService.getCollectPageDetail(businessId);
            return R.success(collectPageDetailVO);
        } catch (BizException biz) {
            return R.fail(biz.getCode(), biz.getMessage());
        }
    }

    @GetMapping("/getCollectRecordById/{recordId}")
    public R<CollectRecordVO> getCollectRecordById(@PathVariable("recordId") Long recordId) {
        CollectRecord collectRecord = collectRecordService.getById(recordId);
        CollectRecordVO collectRecordVO = new CollectRecordVO();
        BeanUtils.copyProperties(collectRecord, collectRecordVO);
        return R.success(collectRecordVO);
    }

}
